﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

using YouHP.Model;
using YouHP.DALFactory;
using YouHP.Utility;

namespace YouHP.BLL
{
    public class SymptomBLL
    {

        #region 单件模式
        static SymptomBLL _Instance;

        public static SymptomBLL Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new SymptomBLL();
                }
                return _Instance;
            }
        }
        #endregion

        #region 症状增删改查基本操作
        public bool AddSymptom(SymptomInfo info)
        {
            return DataAccess.SymptomDAL.AddSymptom(info);
        }

        public bool DeleteSymptom(int[] itemIds)
        {
            return DataAccess.SymptomDAL.DeleteSymptom(itemIds);
        }

        public bool UpdateSymptom(SymptomInfo info)
        {
            return DataAccess.SymptomDAL.UpdateSymptom(info);
        }

#if GENDATA
        public SymptomInfo GetSymptomInfo(int sid)
        {
            SymptomInfo info = new SymptomInfo();
            info.Id = sid;
            info.Name = "高血压";
            info.Intro = "高血压介绍";
            info.Desp = "高血压描述";
            return info;
        }
#else
         public SymptomInfo GetSymptomInfo(int sid)
        {
            return DataAccess.SymptomDAL.GetSymptomInfo(sid);
        }
#endif
        #endregion

        #region 症状列表以及检索操作
        public List<SymptomInfo> GetSymptomList(int pageSize, int pageIndex, out int count)
        {
            return DataAccess.SymptomDAL.GetSymptomList(pageSize, pageIndex,  "", out count);
        }


        public List<SymptomInfo> GetSymptomByName(int pageSize, int pageIndex, string keyword, out int count)
        {
            return DataAccess.SymptomDAL.GetSymptomByName(pageSize, pageIndex, keyword, out count);

        }

        public List<SymptomInfo> GetSymptomByPinYin(int pageSize, int pageIndex, string pinYin,out int count)
        {
            return DataAccess.SymptomDAL.GetSymptomByPinYin(pageSize, pageIndex, pinYin, out count);
        }

        public List<SymptomInfo> GetSymptomByType(int pageSize, int pageIndex, int symptomTypeId, out int count)
        {
            return DataAccess.SymptomDAL.GetSymptomByType(pageSize, pageIndex, symptomTypeId, out count);
        }

        /*检索后台管理所需要的所有症状列表*/
        public List<SymptomInfo> GetAllSymptomByName(string keyword)
        {
            int count = 0;
            return DataAccess.SymptomDAL.GetSymptomByName(Constants.maxPageSize, 1, keyword, out count);
        }  

#if GENDATA
        /// <summary>
        /// 根据条件获取症状列表
        /// </summary>
        public List<SymptomInfo> GetSymptomList(int pageSize, int pageIndex, SymptomSearchConditon ssCond, out int count)
        {
            count = 100;
            List<SymptomInfo> lst = new List<SymptomInfo>();
            for (int i = 1; i < pageSize; i++)
            {
                SymptomInfo info = SymptomBLL.Instance.GetSymptomInfo(i);
                lst.Add(info);
            }
            return lst;
        }
#else
        public List<SymptomInfo> GetSymptomList(int pageSize, int pageIndex, SymptomSearchConditon ssCond, out int count)
        {
            if (ssCond.DiseaseId > 0)
            {
                return DataAccess.SymptomDAL.GetSymptomListWithDiseaseInfo(pageSize, pageIndex, ssCond.ToSqlConditonString(), out count);
            }
            else if(ssCond.SymptomName.Length > 0)
            {
                return DataAccess.SymptomDAL.GetSymptomList(pageSize, pageIndex, ssCond.ToSqlConditonString(), out count);
            }

            return DataAccess.SymptomDAL.GetSymptomList(pageSize, pageIndex, ssCond.ToSqlConditonString(), out count);
        }

        public List<SymptomInfo> GetSymptomList(int pageSize, int pageIndex, SymptomSearchConditon ssCond,string sOrder, out int count)
        {
            if (ssCond.DiseaseId > 0)
            {
                return DataAccess.SymptomDAL.GetSymptomListWithDiseaseInfo(pageSize, pageIndex, ssCond.ToSqlConditonString(), out count);
            }
            else if (ssCond.SymptomName.Length > 0)
            {
                return DataAccess.SymptomDAL.GetSymptomListWithOrder(pageSize, pageIndex, ssCond.ToSqlConditonString(), sOrder, out count);
            }

            return DataAccess.SymptomDAL.GetSymptomListWithOrder(pageSize, pageIndex, ssCond.ToSqlConditonString(),sOrder, out count);
        }
     
       
#endif
        #endregion

        #region 症状类型增删改查基本操作

        public bool AddSymptomType(SymptomTypeInfo info)
        {
            return DataAccess.SymptomDAL.AddSymptomType(info);
        }

        public bool DelSymptomType(int[] itemIds)
        {
            return DataAccess.SymptomDAL.DelSymptomType(itemIds);
        }

        public bool UpdateSymptomType(SymptomTypeInfo info)
        {
            return DataAccess.SymptomDAL.UpdateSymptomType(info);
        }

        public SymptomTypeInfo GetSymptomTypeInfo(int id)
        {
            return DataAccess.SymptomDAL.GetSymptomTypeInfo(id);
        }
        #endregion

        #region 症状类型列表以及检索操作
        public List<SymptomTypeInfo> GetSymptomTypeList()
        {
            return DataAccess.SymptomDAL.GetSymptomTypeList();
        }

        public List<SymptomTypeInfo> GetSymptomTypeList(int parentId)
        {
            return DataAccess.SymptomDAL.GetSymptomTypeList(parentId);
        }

        #endregion

        #region 症状图表统计        
 #if GENDATA
        public List<SymptomStatSeverityCount> GetSymptomStatSeverity(int symid)
        {
            List<SymptomStatSeverityCount> lst = new List<SymptomStatSeverityCount>();

            SymptomStatSeverityCount stat;

            stat = new SymptomStatSeverityCount();
            lst.Add(stat);
            stat.SeverityName = "重度";
            stat.PatientCount = 20;
            
            stat = new SymptomStatSeverityCount();
            lst.Add(stat);
            stat.SeverityName = "中度";
            stat.PatientCount = 30;
            
            stat = new SymptomStatSeverityCount();
            lst.Add(stat);
            stat.SeverityName = "轻度";
            stat.PatientCount = 30;
            
            stat = new SymptomStatSeverityCount();
            lst.Add(stat);
            stat.SeverityName = "无";
            stat.PatientCount = 20;
            
            return lst;
        }
#else
        public List<SymptomStatSeverityCount> GetSymptomStatSeverity(int symId)
        {
            List<SymptomStatSeverityCount> statList = DataAccess.SymptomDAL.GetSymptomStatSeverity(symId);

            //组织症状列表
            for (int i = 0; i < Constants.MAX_SYMPTOM_IDX; i++)
            {
                if ( statList.Count <= i || statList[i].DegreeValue != Constants.MAX_SYMPTOM_IDX - i)
                {
                    SymptomStatSeverityCount statInfo = new SymptomStatSeverityCount();
                    statInfo.PatientCount = 0;
                    statInfo.DegreeValue = Constants.MAX_SYMPTOM_IDX - i;

                    statList.Insert(i, statInfo);
                }
                
                statList[i].colorStr = Constants.SymptomDegreeColor[statList[i].DegreeValue];
                statList[i].SeverityName = Constants.SymptomDegreeName[statList[i].DegreeValue];
            }

            return statList;
        }
#endif

#if GENDATA
        public List<SymptomStatDiseaseCount> GetSymptomStatDisease(int symId)
        {
            List<SymptomStatDiseaseCount> lst = new List<SymptomStatDiseaseCount>();
            for (int i = 0; i < 6; i++)
            {
                SymptomStatDiseaseCount info = new SymptomStatDiseaseCount();
                info.DiseaseId = i * 10 + 1 ;
                info.DiseaseName = "疾病 " + i;
                info.PatientCount = 150 + i * 10;
                lst.Add(info);
            }
            return lst;
        }
#else
        public List<SymptomStatDiseaseCount> GetSymptomStatDisease(int symId)
        {
            return DataAccess.SymptomDAL.GetSymptomStatDisease(symId);
        }
#endif
        #endregion
    }
}
